<template>
  <div class="login-container">
    <el-form :model="form" :rules="rules" ref="form">
      <h3 class="title">
        <img src="../../assets/common/login-logo.png" alt="" />
      </h3>
      <el-form-item prop="user">
        <el-input
          v-model="form.user"
          placeholder="userName"
          prefix-icon="el-icon-user-solid"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="password"
          prefix-icon="el-icon-lock"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="isLoading" type="primary" @click="submit"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { validUser, validPassword } from '../../utils/validate'
import { mapActions } from 'vuex'

export default {
  name: 'login',
  data() {
    return {
      isLoading: false,
      form: {
        user: '13800000002',
        password: '123456'
      },
      rules: {
        user: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { validator: validUser, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions('user', ['login']),
    submit() {
      this.$refs.form.validate(result => {
        if (!result) {
          return console.log(result)
        }

        this.isLoading = true
        this.login({
          mobile: this.form.user,
          password: this.form.password
        })
          .then(res => {
            this.$message({
              showClose: true,
              message: '登录成功',
              type: 'success'
            })
            this.$router.push({ path: '/' })
          })
          .catch(err => {
            if (err === '用户名或密码错误') {
              this.$message({
                showClose: true,
                message: err,
                type: 'error'
              })
              return
            }
            this.$message({
              showClose: true,
              message: err,
              type: 'error'
            })
          })
          .finally(() => {
            this.isLoading = false
          })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../../assets/common/login.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  .title {
    width: 450px;
    margin-bottom: 40px;
    text-align: center;
  }
  .el-form-item {
    background: transparent;
    margin-bottom: 30px;
  }
  ::v-deep .el-input__inner {
    color: #68b0fe;
    font-size: 16px;
    padding: 25px 15px 25px 40px;
    background-color: hsla(0, 0, 100%, 0.8);
    border: 1px solid hsla(0, 0, 100%, 0.1);
  }
  ::v-deep .el-input__inner::placeholder {
    color: #889aa4;
  }
  ::v-deep .el-input__prefix {
    font-size: 18px;
    width: 30px;
    padding-top: 5px;
    color: #889aa4;
  }

  ::v-deep .el-form-item__error {
    color: #fff;
    margin-left: 10px;
  }
  ::v-deep .el-button {
    width: 100%;
    height: 60px;
    padding: 10px 0;
    font-size: 24px;
    background-color: #407ffe;
    color: #fff;
    border: transparent;
  }
}
</style>
